import http from './interface'
import myconfig from '../../config.js'
import mycommon from '../../common.js'

/**
 * 将业务所有接口统一起来便于维护
 * 如果项目很大可以将 url 独立成文件，接口分成不同的模块
 * 
 */



const post = (url, data) => {
	
	
	
	return new Promise((resolve, reject) => {
		
		reject({
			code: 1,
			msg: 'eee'
		})
	}).catch((e) => {});
	

	http.config.baseUrl = "https://shop.mmno.com/api.php"
	//设置请求前拦截器
	http.interceptor.request = (config) => {
		config.header = {
			'Content-Type': 'application/json;charset=UTF-8',
			'Content-Type': 'application/x-www-form-urlencoded',
			"tk-token": uni.getStorageSync('token')
		}
	}
	 
	
	//设置请求结束后拦截器
	// 死活拦截不了,return false后,程序还是会往下执行.换用pocky-request了
	http.interceptor.response = (response) => {
	
		if (response.statusCode === 200) {
	
			if (response.data.code > 0) {
								
				uni.showToast({
					title: response.data.msg,
					duration: 2000,
					icon:'none'
				})
				
				return Promise.resolve(null);
			} else if (response.data.code === -1) {
				mycommon.navigateTo('../everyone/login')
				return Promise.resolve(null);
			}
	
		} else {
			uni.showToast({
				title: '网络出错',
				icon: 'none'
			});
			return Promise.resolve(null);
	
		}
		return response;
	}
	
	return http.request({
		method: 'POST',
		url: url,
		data: data
	})
}

// 单独导出(测试接口) import {test} from '@/common/vmeitime-http/'
export const test = (data) => {
	/* http.config.baseUrl = "http://localhost:8080/api/"
	//设置请求前拦截器
	http.interceptor.request = (config) => {
		config.header = {
			"token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
		}
	} */
	//设置请求结束后拦截器
	http.interceptor.response = (response) => {
		console.log('个性化response....')
		//判断返回状态 执行相应操作
		return response;
	}
	return http.request({
		baseUrl: 'https://unidemo.dcloud.net.cn/',
		url: 'ajax/echo/text?name=uni-app',
		dataType: 'text',
		data,
	})
}

// 轮播图
export const banner = (data) => {
	return http.request({
		url: '/banner/36kr',
		method: 'GET',
		data,
		// handle:true
	})
}

// 默认全部导出  import api from '@/common/vmeitime-http/'
export default {	
	post
}
